/*
 * Created by JFormDesigner on Fri Oct 20 22:21:38 CST 2023
 */

package cn.edu.cdp.gui.panel;

import java.awt.event.*;
import java.util.*;

import cn.edu.cdp.dao.CourseDao;
import cn.edu.cdp.dao.StudyDao;
import cn.edu.cdp.domain.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author fanyi
 */
@Component
public class TeacherPanel extends JPanel {
    private Course course;

    private final CourseDao courseDao;
    private final StudyDao studyDao;

    @Autowired
    public TeacherPanel(CourseDao courseDao, StudyDao studyDao) {
        initComponents();
        this.courseDao = courseDao;
        this.studyDao = studyDao;
    }

    public void decoration(Course course) {
        this.course = course;
        textFieldId.setText(course.getCourseId());
        textFieldName.setText(course.getName());
        textFieldCredit.setText(course.getCredit().toString());

        //======== scrollPane1 ========
        refreshData();
    }

    public void refreshData() {
        List<Map<String, Object>> scoreList = courseDao.scoreList(course.getCourseId());
        Vector<Vector<String>> tableData = new Vector<>();
        for (Map<String, Object> map : scoreList) {
            Vector<String> row = new Vector<>();
            row.add((String) map.get("studentId"));
            row.add((String) map.get("name"));
            row.add(map.get("score").toString());
            tableData.add(row);
        }

        Vector<String> tableTitle = new Vector<>();
        tableTitle.add("学号");
        tableTitle.add("学生姓名");
        tableTitle.add("成绩");
        //---- table1 ----
        table1.setModel(new DefaultTableModel(tableData, tableTitle) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;   // 禁用在表格中编辑
            }
        });
    }

    private void table1MouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            int row = table1.getSelectedRow();
            String studentId = (String) table1.getValueAt(row, 0);
            String s = JOptionPane.showInputDialog(this, "请输入修改分数：");
            if (s != null && !s.isEmpty()) {
                double score = Double.parseDouble(s);
                studyDao.updateScore(studentId, this.course.getCourseId(), score);
                refreshData();
            }
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        ResourceBundle bundle = ResourceBundle.getBundle("swing_text");
        label1 = new JLabel();
        textFieldId = new JTextField();
        label2 = new JLabel();
        textFieldName = new JTextField();
        label3 = new JLabel();
        textFieldCredit = new JTextField();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setLayout(new GridBagLayout());
        ((GridBagLayout)getLayout()).columnWidths = new int[] {25, 65, 125, 65, 125, 65, 125, 20, 0};
        ((GridBagLayout)getLayout()).rowHeights = new int[] {25, 35, 25, 200, 0};
        ((GridBagLayout)getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
        ((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 1.0E-4};

        //---- label1 ----
        label1.setText(bundle.getString("TeacherPanel.label1.text"));
        add(label1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- textFieldId ----
        textFieldId.setEditable(false);
        textFieldId.setRequestFocusEnabled(false);
        textFieldId.setFocusable(false);
        add(textFieldId, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- label2 ----
        label2.setText(bundle.getString("TeacherPanel.label2.text"));
        add(label2, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- textFieldName ----
        textFieldName.setEditable(false);
        textFieldName.setRequestFocusEnabled(false);
        textFieldName.setFocusable(false);
        add(textFieldName, new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- label3 ----
        label3.setText(bundle.getString("TeacherPanel.label3.text"));
        add(label3, new GridBagConstraints(5, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- textFieldCredit ----
        textFieldCredit.setEditable(false);
        textFieldCredit.setRequestFocusEnabled(false);
        textFieldCredit.setFocusable(false);
        add(textFieldCredit, new GridBagConstraints(6, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1, new GridBagConstraints(1, 3, 6, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 5), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JTextField textFieldId;
    private JLabel label2;
    private JTextField textFieldName;
    private JLabel label3;
    private JTextField textFieldCredit;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
